package com.feige.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feige.constant.Constants;
import com.feige.pojo.SignTask;
import com.feige.service.SignTaskService;
import com.feige.result.AjaxResult;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  签到任务 前端控制器
 * </p>
 *
 * @author feige
 * @since 2020-11-20
 */
@Api(tags = "签到任务 前端控制器")
@RestController
@RequestMapping("/sign/sign-task")
public class SignTaskController {
    @Resource
    private SignTaskService signTaskService;


    @GetMapping("/get/{pageNum}/{courseId}")
    public AjaxResult getSignTaskByCourseId(@PathVariable String courseId, @PathVariable Integer pageNum){
        IPage<SignTask> signTaskPage = new Page<>(pageNum, Constants.PAGE_SIZE);
        QueryWrapper<SignTask> wrapper = new QueryWrapper<>();
        wrapper
                .select("id" ,"type","sign_begin_time","sign_end_time","gmt_create","extend")
                .eq("course_id",courseId)
                .orderByDesc("gmt_create");
        IPage<SignTask> page = signTaskService.page(signTaskPage, wrapper);
        List<SignTask> list = page.getRecords();
        long total = page.getTotal();
        AjaxResult success = AjaxResult.success(list);
        success.put("total",total);
        return success;
    }

    @GetMapping("/get/{id}")
    public AjaxResult getSignTaskById(@PathVariable String id){
        SignTask signTask = signTaskService.getById(id);
        if (ObjectUtil.isNotEmpty(signTask)){
            return AjaxResult.success(signTask);
        }else {
            return AjaxResult.error(5005,"未查询到数据！");
        }
    }

    @GetMapping("/s/{courseId}/{pageNum}/{userId}")
    public AjaxResult getNotifyByUserId(@PathVariable("courseId") String courseId, @PathVariable("pageNum") Long pageNum, @PathVariable("userId") String userId){
        return signTaskService.getSignStatus(userId,courseId,pageNum);
    }

    @PostMapping("/save")
    public AjaxResult save(@RequestBody SignTask signTask){
        boolean save = signTaskService.save(signTask);
        if (save){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
}

